import React from 'react';
import {connect} from 'dva';
import PropTypes from 'prop-types';
import {Table, Button,Row,Col,Layout, Breadcrumb,Menu,Dropdown,Popconfirm,Icon,Input} from 'antd';
import style from '../../styles/TableCss.less';
import {Link} from 'dva/router'
import reqwest from 'reqwest';
import {datevali} from '../../utils/Validator.js'
const {  Content, Footer } = Layout;
let start_at=null;
let end_at=null;
class Strate extends React.Component{
  constructor(props){
    super(props);
    let _this=this;
    this.state={
      data:[],
      dropDown:'所有线索',
      columns:[{
        title:'主题',
        dataIndex:'title',
        width:'20%'
      },{
        title:'策略',
        dataIndex:'str',
      },{
        title:'时间',
        dataIndex:'timer',
      },{
        title:'优先级',
        dataIndex:'pound'
      },{
        title:'操作',
        key:'action',
        render:(text,record) => (
          <span onClick={(e) => this.editClick(record,text, e,_this)}>
            <Link  to={{pathname:'/app/editstr',query:{current:this.props.location.query.current,initv:'无敌'}}}>编辑</Link>
          </span>
        )
      }],
    }
  }
  editClick(record,text, e,_this){
    console.log(text)
    sessionStorage['edit']=JSON.stringify({id:record.key,
      title:record.title,rate:record.str.props.children[1],
      discount:record.str.props.children[3],
      rebate:record.str.props.children[5],
      // start_at:record.timer.props.children[0].props.children,
      // end_at:record.timer.props.children[2].props.children,
      start_at:record.startTimer,
      end_at:record.endTimer,
      address:[record.province,record.city,record.area],
      regions_id:record.regions_id,
      pound:record.pound,
      remark:record.remark
    });
  }
  handleTableChange(pagination, filters, sorter){
    const pager = { ...this.state.pagination };
    // console.log(pagination.current)
    pager.current = pagination.current;
    this.setState({
      pagination: pager,
    });
    this.fetch({
      results: pagination.pageSize,
      page: pagination.current,
      sortField: sorter.field,
      sortOrder: sorter.order,
      ...filters,
    });
  }
  fetch (params = {}) {
    console.log('params:', params);
    let _data=[];
    
    this.setState({ loading: true });
    reqwest({
      url: window.host+'/api/strategies',
      method: 'get',
      crossOrigin: true,
      data: {
        access_token:localStorage['token'],
        page:params.page,
      },
      type: 'json',
    }).then((data) => {
      console.log(data);
        const pagination = { ...this.state.pagination };
        pagination.total = data.total;
        sessionStorage['regions']=JSON.stringify(data.regions);
        for(let i of data.list){
          start_at =datevali(i.start_at);
          end_at=datevali(i.end_at);
          _data.push({
            key:i.id,
            title:i.title,
            str:<span>提成:{i.rate}/折扣:{i.discount}/返利:{i.rebate}</span>,
            timer:<div><span>{start_at}</span>-<span>{end_at}</span></div>,
            startTimer:i.start_at,
            endTimer:i.end_at,
            province:i.province,
            city:i.city,
            area:i.area,
            regions_id:i.region_id,
            pound:i.pound,
            remark:i.remark
          })
      }
      this.setState({
        loading: false,
        data: _data,
        pagination,
      });
    }).fail( (err, msg) =>{
       console.log(1)
    })
  }
  componentWillMount(){
    this.fetch({page:1});
    console.log(this.props.location.current)
  }
  menuClick(e){
    console.log(e.item.props.value)
    this.setState({dropDown:e.item.props.value});
  }
  render(){
    const { selectedRowKeys,data,pagination,loading,dropDown,columns } = this.state;

      // <a target="_blank" rel="noopener noreferrer" href="http://www.alipay.com/">所有客户</a>
    const customerMemu=(
      <Menu onClick={this.menuClick.bind(this) } selectedKeys={[dropDown]}>
        <Menu.Item value='所有线索'>
          <a target="_blank" rel="noopener noreferrer">所有线索</a>
        </Menu.Item>
        <Menu.Item value='我的线索'>
          <a target="_blank" rel="noopener noreferrer" >我的线索</a>
        </Menu.Item>
        <Menu.Item value='aaaa'>
          <a target="_blank" rel="noopener noreferrer">aaaa</a>
        </Menu.Item>
      </Menu>
    )
    return(
      <Layout style={{background:'#fff',height:'100%' }}>
        <Content>
          <Row type='flex'>
            <Col span={7} xl={{span:7}} xs={7} style={{padding:'24px 25px 0px'}}>
              <Breadcrumb style={{fontSize:14}}>
                <Breadcrumb.Item>策略</Breadcrumb.Item>
              </Breadcrumb>
            </Col>
            <Col xl={{span :3,offset:13}} lg={{span:3,offset:13}} md={{span:3,offset:13}} sm={{span:3,offset:13}} style={{lineHeight:'60px',textAlign:'right'}}>
              <Button ><Link to={{pathname:'/app/newstr',query:{current:this.props.location.query.current}}}>新建策略</Link></Button>
            </Col>
          </Row>
          <Layout className={`${style.tb_serbor}`}>
            <Row type='flex' className={`${style.tb_ser}`}>
              <Col span={6}>
                
              </Col>
              <Col span={7} offset={11} style={{textAlign:'right'}}>
               
              </Col>
            </Row>
            <Table columns={columns}
            key='ttt'
              dataSource={data} onRowClick={this.handleRowClick}
              pagination={pagination}
              loading={loading}
              onChange={this.handleTableChange.bind(this)}
            />
          </Layout>
        </Content>
      </Layout> 
    )
  }
}
 // <Input.Search 
                //   placeholder="input search text"
                //   style={{ width: 200 }}
                //   onSearch={value => console.log(value)}
                //  />
export default connect()(Strate);